extends NodeState


var player: Player:
	get:
		return state_matchine.root as Player


func _on_process(_delta : float) -> void:
	# 旋转队友检测区域
	if player.velocity != Vector2.ZERO:
		player.teammate_detection_area.rotation = player.velocity.angle()

	process_animation()
	process_input()



func _on_enter(_data: NodeStateData) -> void:
	player.ball_detection_area.body_entered.connect(on_ball_entered)


func _on_exit() -> void:
	player.ball_detection_area.body_entered.disconnect(on_ball_entered)



func process_animation():
	player.animation_player.play(Player.ANIMATION_RUN)


func process_input():
	player.velocity = player.input_manager.movement_direction * player.speed
	player.move_and_slide()

	# 射门
	if player.is_carrying_ball() and player.input_manager.is_shoot_pressed():
		transition_state(Player.STATE_PREPPING_SHOT)
		return
	# 传球
	if player.is_carrying_ball() and player.input_manager.is_pass_pressed():
		transition_state(Player.STATE_PASSING)
		return

	if not player.input_manager.is_movement():
		transition_state(Player.STATE_IDLE)
		return
	# 铲球
	if player.input_manager.is_tackling() and not player.is_carrying_ball() and player.ball.is_carried():
		transition_state(Player.STATE_TACKLING)
		return


func on_ball_entered(body: Node2D):
	if not body is Ball:
		return
	var entered_ball: Ball = body as Ball

	if not entered_ball.can_air_interactive():
		return
	if not player.input_manager.is_shooting():
		return
	transition_state(Player.STATE_HEADER)
